package com.one.shopping.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.one.shopping.R;
import com.one.shopping.api.OneApi;
import com.one.shopping.entity.Member;
import com.one.shopping.entity.Result;
import com.one.shopping.ui.baseActivity.BaseActivity;
import com.one.shopping.util.CommonUtils;
import com.one.shopping.util.GsonUtils;

import org.apache.http.Header;

/**
 * @author 柯尊诚
 * @version 1.0
 * @date 创建时间：2016/1/12 17:52
 */
public class AddMemberActivity extends BaseActivity{

    private EditText nameField;
    private EditText phoneField;
    private EditText scoreField;
    private EditText moneyField;
    private TextView idValues;
    private Button submitBtn;
    private boolean isUpdate;

    @Override
    protected int getContentViewId() {
        return R.layout.add_member_activity;
    }

    @Override
    protected void initView() {
        super.initView();
        nameField = findView(R.id.add_member_name_field_et);
        phoneField = findView(R.id.add_member_phone_field_et);
        scoreField = findView(R.id.add_member_score_field_et);
        moneyField = findView(R.id.add_member_money_field_et);
        idValues = findView(R.id.add_member_cardid_values_tv);
        submitBtn = findView(R.id.add_member_sumit_btn);
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        Member member = (Member) intent.getSerializableExtra("member");
        if (CommonUtils.isNullOrEmpty(member)) {
            setTitle("添加会员");
            findView(R.id.add_member_id_layout).setVisibility(View.GONE);
        } else {
            setTitle("修改会员");
            isUpdate = true;
            idValues.setText("" + member.getCardId());
            nameField.setText(member.getUserName());
            phoneField.setText(member.getPhone());
            scoreField.setText("" + member.getScore());
            moneyField.setText("" + member.getMoney());

        }
    }

    @Override
    protected void initListener() {
        super.initListener();

        submitBtn.setOnClickListener(onClickSubmitListener);
    }

    /**
     * 点击提交按钮
     */
    private View.OnClickListener onClickSubmitListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onNextSubmit();
        }
    };

    /**
     * 处理点击提交
     */
    private void onNextSubmit() {
        String name = nameField.getText().toString().trim();
        String phone = phoneField.getText().toString().trim();
        String score = scoreField.getText().toString().trim();
        String money = moneyField.getText().toString().trim();
        if (CommonUtils.isNullOrEmpty(name)) {
            nameField.setError("用户名不能为空");
            return;
        }
        if (CommonUtils.isNullOrEmpty(phone)) {
            phoneField.setError("电话号码不能为空");
            return;
        }
        if (CommonUtils.isNullOrEmpty(score)) {
            scoreField.setError("积分不能为空");
            return;
        }
        if (CommonUtils.isNullOrEmpty(money)) {
            moneyField.setError("余额不能为空");
            return;
        }

        Member member = new Member();
        member.setUserName(name);
        member.setPhone(phone);
        member.setScore(score);
        member.setMoney(money);

        if (isUpdate) {
            int cardId = Integer.parseInt(idValues.getText().toString().trim());
            member.setCardId(cardId);
            OneApi.updateMember(member, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    Result result = GsonUtils.fromJson(new String(bytes), Result.class);
                    makeToast(result.getMessage());
                    finish();
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                    makeToast("网络错误！");
                }
            });
        } else {
            OneApi.registerMember(member, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    Result result = GsonUtils.fromJson(new String(bytes), Result.class);
                    if (result.getCode() == 500) {
                        makeToast(result.getMessage());
                    } else {
                        makeToast(result.getMessage());
                        finish();
                    }
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                    makeToast("网络错误！");
                }
            });
        }
    }
}
